package com.example.zhu29.bledemo.util;

/**
   * Author: Huang Qi
   * Time: 2018-10-29 10:33
   */
public class ZDataUtil {

    /**
           * Convert a string to a hex string
           *
           * @param str str ASCII string to be converted
           * @return String Separated by spaces between each Byte, such as: [61 6C 6B]
           */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
           * hexadecimal conversion string
           *
           * @param hexStr str Byte string (no separator between Bytes such as: [616C6B])
           * @return String Corresponding string
           */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
           * Functional analysis:
           * Position 0. Start code: Downstream header, fixed to constant: 5AH.
           * Location 1. Packet length: The length of the entire package, including the CheckSum section.
           * Location 2. Package Category: Downstream Command Type
           * <p>
           * 00H Read blood sputum information.
           * 01H sphygmomanometer starts measurement
           * 03H Read blood glucose meter measurement data.
           * 09H handshake package.
           * : Reserved.
           * <p>
           * Location 3-8. System Time: The current time of the intelligent system, used to update the bloody time.
           * Location 9.Check_sum: Checksum, starting from the start code, the sum of all bytes between Check_sum +2, taking the low byte, the high byte is discarded.
           *
           * @param sendPack
           * @return
           */
    public static String getSendHex(int sendPack) {
        String time = ZTimeTool.getCurrentDateTime("yy-MM-dd-HH-mm-ss");
        String[] arrTime = time.split("-");
        if (arrTime.length == 6) {
            int index0 = 90;
            int index1 = 10;
            int index2 = sendPack;//00 package 03package
            int index3 = Integer.parseInt(arrTime[0]);//year
            int index4 = Integer.parseInt(arrTime[1]);//month
            int index5 = Integer.parseInt(arrTime[2]);//day
            int index6 = Integer.parseInt(arrTime[3]);//Time
            int index7 = Integer.parseInt(arrTime[4]);//Minute
            int index8 = Integer.parseInt(arrTime[5]);//second
            int index9 = index0 + index1 + index2 + index3 + index4 + index5 + index6 + index7 + index8 + 2;

            byte[] data = new byte[]{(byte) index0, (byte) index1, (byte) index2, (byte) index3,
                    (byte) index4, (byte) index5, (byte) index6, (byte) index7, (byte) index8};

            String hexSend = ZHexUtil.encodeHexStr(data);
            String hexIndex9 = ZHexUtil.encodeHexStr(new byte[]{(byte) index9});
            int len = hexIndex9.length();
            String i9 = hexIndex9.substring(len - 2, len);
            return String.format("%s%s", hexSend, i9).toUpperCase();
        }
        return "";
    }

    /**
           * Format: Start code Packet length Package category Year Month Day Time Minutes Seconds Check_sum
           Offset 0 1 2 3 4 5 6 7 8 9
           Value range 5AH 00H-FFH 00H-FFH 00H~63H 01H-0CH 01H-1FH 00H-17H 00H-3BH 00H-3BH 00~FF

           */
    public static String startPackage() {
        int index0 = 90;
        int index1 = 6;
        int index2 = 1;// 01package
        int index3 = 0;
        int index4 = 0;
        int index5 = 92;

        byte[] data = new byte[]{(byte) index0, (byte) index1, (byte) index2, (byte) index3,
                (byte) index4, (byte) index5};

        String hexSend = ZHexUtil.encodeHexStr(data);
        return hexSend.toUpperCase();
    }

}
